var ns;
var app = new Vue({
    el: '#app',
    data: {
        isSearch: true,
        id: 0,
        form: {
            ID: 0,
        },
        visitor: [{
            id: '1',
            name: 'A小区',
            visit: '朋友来访',
            residual: '3',
            beDate: '2019.4.28',
            endDate: '2019.6.1'
        }],
        myDate:'',
        beginDate:'',
        endDate:'',
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'getscanvisitqrcodedata',
                ID: that.form.ID
            }, function(succeed, data, err) {
                if (succeed) {
                    that.form = data;
                    myDate = new Date();
                    beginDate = new Date(that.form.StartTime.replace('年','-').replace('月','-').replace('日',''));
                    endDate = new Date(that.form.EndTime.replace('年','-').replace('月','-').replace('日','')+' 23:59:59');
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        current: function() {
          var that = this;
            api.confirm({
                title: '提示',
                msg: '是否允许本次通行',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex==1) {
                    if(that.form.UseRestCount <= 0){
                      api.toast({
                          msg: '剩余次数为0不能再次通行',
                          duration: 2000,
                          location: 'bottom'
                      });
                      return false;
                    };
                    //日期判断
                    if(myDate<beginDate){
                    	api.toast({
                    	    msg: '访客时间还没开始',
                    	    duration: 2000,
                    	    location: 'bottom'
                    	});
                      return false;
                    }
                    if(myDate>endDate){
                      api.toast({
                    	    msg: '访客时间已经结束',
                    	    duration: 2000,
                    	    location: 'bottom'
                    	});
                      return false;
                    }
                    ns.post({
                        action: 'savescanvisitlog',
                        ID: that.form.ID
                    }, function(succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '操作成功',
                                duration: 2000,
                                location: 'bottom'
                            });
                            setTimeout(function() {
                                api.closeWin();
                            }, 500);
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    });
                }
            });
        },
    }
});
apiready = function() {
    api.parseTapmode();
    app.form.ID = api.pageParam.id; //二维码传值
    ns = window.Foresight.Util;
    app.get_data();
    setTimeout(function() {
        api.closeFrame({
            name: 'scanner_frm'
        });
    }, 500);
}
